'''
陈超依 19377189
现代程序设计第六次作业
'''

import os
from PIL import Image as Im
from PIL import ImageFilter as ImF
import matplotlib.pyplot as plt

#基类Filter
class Filter:
    def __init__(self, image : Im.Image, Largus : list):
        self._image = image
        self._Largus = Largus
    def filter(self):
        pass

#基类Filter的若干子类
class Filter_find_edges(Filter):
    '''边缘提取'''
    def __init__(self, image : Im.Image, Largus : list):
        Filter.__init__(self, image, Largus)
    def filter(self):
        return self._image.filter(ImF.FIND_EDGES)

class Filter_sharpen(Filter):
    '''锐化滤波'''
    def __init__(self, image : Im.Image, Largus : list):
        Filter.__init__(self, image, Largus)
    def filter(self):
        return self._image.filter(ImF.SHARPEN)

class Filter_blur(Filter):
    '''模糊滤波'''
    def __init__(self, image : Im.Image, Largus : list):
        Filter.__init__(self, image, Largus)
    def filter(self):
        return self._image.filter(ImF.BLUR)

class Filter_resize(Filter):
    '''大小调整'''
    def __init__(self, image : Im.Image, Largus : list):
        Filter.__init__(self, image, Largus)
    def filter(self):
        return self._image.filter(self._image.resize(self._Largus))

#类ImageShop
class ImageShop:
    def __init__(self, Imformat : str, Imfile : str, Imlist=[]):
        '''Imformat图片格式，Imfile图片文件/目录,Imlist储存文件目录'''
        self._Imformat = Imformat
        self._Imfile = Imfile
        self._Imlist = Imlist
    def load_images(self):
        tempfile = self._Imfile[:]
        if self._Imformat == (tempfile.split('.'))[-1]:
            self._Imlist.append(Im.open(self._Imfile))
        else:
            files = os.listdir(self._Imfile)
            for f in files:
                if self._Imformat == (f.split('.'))[-1]:
                    self._Imlist.append(Im.open(f))
        return self._Imlist
    def __batch_ps(self, filter : Filter, Largus=[]):
        for i in range(len(self._Imlist)):
            temp_filter = filter(self._Imlist[i], Largus)
            self._Imlist[i] = (temp_filter.filter())
        return self._Imlist
    def batch_ps(self, Largus : list):
        '''Largus 应为元组的列表'''
        for tup in Largus:
            try:
                tup[1]
                if (tup[0] == 'resize'):
                    self.__batch_ps(Filter_resize, tup[1])
            except IndexError:
                if (tup[0] == 'find_edges'):
                    self.__batch_ps(Filter_find_edges)
                if (tup[0] == 'sharpen'):
                    self.__batch_ps(Filter_sharpen)
                if (tup[0] == 'blur'):
                    self.__batch_ps(Filter_blur)
    def display(self, row=1, col=1, length=100, width=100, maxnum=9):
        plt.figure(figsize=(col*width, row*length))
        num = min(maxnum, len(self._Imlist))
        for i in range(num):
            plt.subplot(row, col, i+1)
            plt.imshow(self._Imlist[i])
            plt.axis('off')
        plt.show()
    def save(self, dis, format):
        for i in range(len(self._Imlist)):
            self._Imlist[i].save(dis+'\\'+'result'+str(i+1)+'.'+format, format)

class TestImageShop:
    def __init__(self, Imfile : str, Imformat : str, Lcommand : list, savedis : str):
        self._Imfile = Imfile
        self._Lcommand = Lcommand
        self._Imformat = Imformat
        self._save = savedis
    def _test(self):
        test = ImageShop(self._Imformat, self._Imfile)
        test.load_images()
        test.batch_ps(self._Lcommand)
        test.display()
        test.save(self._save, self._Imformat)

def main():
    test = TestImageShop('E:\\Py_Programs\\week6\\test.png', 'png',\
        [('blur',)], 'E:\\Py_Programs\\week6')
    test._test()

if __name__ == '__main__':
    main()